import re # 必写

#1 match
pattern1 = r'\d\.\d+'
str1 = 'I study Python 3.11 every day'
match1 = re.match(pattern1, str1, re.I) # re.I 忽略大小写
# match1 = re.match(r'\d\.\d+', str1, re.I) # 等义
# print(match1) # 开始位置匹配，所以找不到 None

str2 = '3.11 Python，I study  every day. - 5.21'
match2 = re.match(r'\d\.\d+', str2, re.I)
print(match2)
# <re.Match object; span=(0, 4), match='3.11'>
print('匹配的起始位置：', match2.start())
# 匹配的起始位置： 0
print('匹配的结束位置：', match2.end())
# 匹配的结束位置： 4
print('匹配的区间：', match2.span())
# 匹配的区间： (0, 4)
print('匹配的源字符串：', match2.string)
# 匹配的源字符串： 3.11 Python，I study  every day. - 5.21
print('匹配的字符串：', match2.group())
# 匹配的字符串： 3.11

#2 search
str3 = 'I study Python 3.11 every day. Python 2.7, i love you.'
match3 = re.search(r'\d\.\d+', str3)
print(match3) # 搜索第一个匹配的值
# <re.Match object; span=(15, 19), match='3.11'>

#3 findall
str4 = 'I study Python 3.11 every day. Python 2.7, i love you.'
lst4 = re.findall(r'\d\.\d+', str4) # 输出为列表
print(lst4)
# ['3.11', '2.7']

#4 sub
str5 = '我想学习Python，想破解一些VIP视频，Python可以实现无底线反爬吗？'
new_str5 = re.sub(r'黑客|破解|反爬', '***',str5)
print(new_str5)
# 我想学习Python，想***一些VIP视频，Python可以实现无底线***吗？

#5 split
str6 = 'https://www.quark.cn/s?q=python'
lst6 = re.split(r'[:?]', str6)  # 用 :号，?号切割字符串
print(lst6)
# ['https', '//www.quark.cn/s', 'q=python']
